package com.boyinet.activation.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boyinet.activation.entity.SoftwareInfo;
import com.boyinet.activation.service.SoftwareInfoService;
import com.boyinet.activation.util.MD5Util;
import com.boyinet.activation.util.R;
import com.boyinet.activation.vo.SoftwareInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/software")
public class SoftWareController {

    @Autowired
   private SoftwareInfoService userInfoService;

    @RequestMapping("/save")
    public R save(@RequestBody SoftwareInfo userInfo){
        if (StrUtil.isNotBlank(userInfo.getMac())){
            String md5 = MD5Util.md5Encrypt32Lower(userInfo.getMac());
            String SN = MD5Util.md5Encrypt32Lower(md5);
            userInfo.setSn(SN);
            userInfo.setCreateTime(new Date());
            userInfoService.save(userInfo);
            return R.ok(SN);
        }
        return R.error("Mac 机器码为空");
    }

    @RequestMapping("/list")
    public R listSoftwareInfo(@RequestBody SoftwareInfoVO condition){
        Page<SoftwareInfo> list = userInfoService.listByPage(condition);
        Map<String, Object> map = new HashMap<>(4);
        map.put("count", list.getTotal());
        map.put("pi", condition.getPage());
        map.put("ps", condition.getLimit());
        map.put("data", list.getRecords());
        return R.ok(map);
    }
}
